package com.ruoyi.web.controller.system;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.domain.Purchase;
import com.ruoyi.system.domain.PurchaseRecord;
import com.ruoyi.system.service.AnalysisService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/system/analysis")
public class AnalysisController extends BaseController {
    @Autowired
    private AnalysisService AnalysisService;

    //各厂商价格对比
    @GetMapping("/comparison")
    public TableDataInfo comparison(PurchaseRecord purchaseRecord) {
        //开启分页
        startPage();
        List<PurchaseRecord> list = AnalysisService.selectComparison(purchaseRecord);
        return getDataTable(list);
    }

    @GetMapping("/invoice")
    public TableDataInfo invoice(Integer choose) {
        startPage();
        List<Purchase> list = AnalysisService.selectInvoiceList(choose);
        return getDataTable(list);
    }

    //根据时间
    @GetMapping("/time")
    public TableDataInfo time(String startTime, String endTime) {
        startPage();
        List<PurchaseRecord> list = AnalysisService.selectTimeList(startTime, endTime);
        return getDataTable(list);
    }

    //各厂商价格对比
    @GetMapping("/comparison/export")
    public void comparisonexport(PurchaseRecord purchaseRecord, HttpServletResponse response) {
        try {
            List<PurchaseRecord> purchaseRecords = AnalysisService.selectComparison(purchaseRecord);
            List list1 = new ArrayList();
            for (int i = 0; i < purchaseRecords.size(); i++) {
                Map map = new HashMap();
                map.put("id", i + 1);
                map.put("mName", purchaseRecords.get(i).getmName());
                map.put("mModel", purchaseRecords.get(i).getmModel());
                map.put("mUnit", purchaseRecords.get(i).getmUnit());
                map.put("mdSpecifications", purchaseRecords.get(i).getMdSpecifications());
                map.put("mdSum", purchaseRecords.get(i).getMdSum());
                map.put("mdPrice", purchaseRecords.get(i).getMdPrice());
                map.put("truePrice", purchaseRecords.get(i).getTruePrice());
                map.put("mdBusiness", purchaseRecords.get(i).getMdSum());
                map.put("requisitionDate", purchaseRecords.get(i).getRequisitionDate());
                if (null != purchaseRecords.get(i).getCommBus()) {
                    if (purchaseRecords.get(i).getCommBus().equals("0")) map.put("commBus", "否");
                    if (purchaseRecords.get(i).getCommBus().equals("1")) map.put("commBus", "是");
                }
                map.put("mdBrand", purchaseRecords.get(i).getMdBrand());
//                if (purchaseRecords.get(i).getSatetOut() == 1) map.put("state", "已出库");
//                if (purchaseRecords.get(i).getSatetOut() == 0) map.put("state", "未出库");
//                if (purchaseRecords.get(i).getStateIn() == 1) map.put("state", "已入库");
//                if (purchaseRecords.get(i).getStateIn() == 0) map.put("state", "未入库");
                map.put("wareName", purchaseRecords.get(i).getWareName());
                list1.add(map);
            }
            Map map = new HashMap();
            // 表格使用的数据
            map.put("data", list1);//放入表一数据
            // 获取模板文件
            //读取模板
            TemplateExportParams params = new TemplateExportParams("/home/ruoyi/excel/采购单价格分析.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/time/export")
    public void timeexport(String startTime, String endTime, HttpServletResponse response) {
        try {
            List<PurchaseRecord> list = AnalysisService.selectTimeList(startTime, endTime);
            List list1 = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                Map map = new HashMap();
                map.put("id", i + 1);
                map.put("mName", list.get(i).getmName());
                map.put("mModel", list.get(i).getmModel());
                map.put("mUnit", list.get(i).getmUnit());
                map.put("mdSpecifications", list.get(i).getMdSpecifications());
                map.put("mdSum", list.get(i).getMdSum());
                map.put("mdPrice", list.get(i).getMdPrice());
                map.put("truePrice", list.get(i).getTruePrice());
                map.put("mdBusiness", list.get(i).getMdBusiness());
                if (null != list.get(i).getCommBus()) {
                    if (list.get(i).getCommBus().equals("0")) map.put("commBus", "是");
                    if (list.get(i).getCommBus().equals("1")) map.put("commBus", "否");
                }
                map.put("mdBrand", list.get(i).getMdBrand());
                map.put("requisitionDate", list.get(i).getRequisitionDate());
                map.put("wareName", list.get(i).getWareName());
                list1.add(map);
            }
            Map map = new HashMap();
            // 表格使用的数据
            map.put("data", list1);//放入表一数据
            map.put("startTime", startTime);//放入表一数据
            map.put("endTime", endTime);//放入表一数据
            // 获取模板文件
            //读取模板
            TemplateExportParams params = new TemplateExportParams("/home/ruoyi/excel/采购单时间.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //返回同一项目的所有采购材料
    @GetMapping("/project")
    public TableDataInfo project(Project project) {
        //开启分页
        startPage();
        List<PurchaseRecord> list = AnalysisService.selectProject(project);
        return getDataTable(list);
    }

    //返回同一项目的所有采购材料
    @GetMapping("/project/export")
    public void export(Project project, HttpServletResponse response) {
        List<PurchaseRecord> purchaseRecords = AnalysisService.selectProject(project);
        List<Object> list = new ArrayList<>();
        // 表一数据
        try {
            DecimalFormat df = new DecimalFormat("#.##");
            df.setRoundingMode(RoundingMode.HALF_UP);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < purchaseRecords.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", i + 1);
                if (null != purchaseRecords.get(i).getProName()) {
                    map.put("proName", purchaseRecords.get(i).getProName());
                } else map.put("proName", "无");
                if (null != purchaseRecords.get(i).getProNum()) {
                    map.put("proNum", purchaseRecords.get(i).getProNum());
                } else map.put("proNum", "无");
                if (null != purchaseRecords.get(i).getmName())
                    map.put("mName", purchaseRecords.get(i).getmName());
                else map.put("mName", "无");
                if (null != purchaseRecords.get(i).getMdSpecifications() && null != purchaseRecords.get(i).getMdSpecifications()) {
                    map.put("mdSpecifications", purchaseRecords.get(i).getMdSpecifications());
                } else map.put("mdSpecifications", "无");
                if (null != purchaseRecords.get(i).getMdSpecifications()) {
                    map.put("mModel", purchaseRecords.get(i).getmModel());
                } else map.put("mModel", "无");
                if (null != purchaseRecords.get(i).getmUnit())
                    map.put("mUnit", purchaseRecords.get(i).getmUnit());
                else map.put("mUnit", "无");
                if (null != purchaseRecords.get(i).getMdBrand())
                    map.put("mdBrand", purchaseRecords.get(i).getMdBrand());
                else map.put("mdBrand", "无");
                if (null != purchaseRecords.get(i).getMdBusiness())
                    map.put("mdBusiness", purchaseRecords.get(i).getMdBusiness());
                else map.put("mdBusiness", "无");
                if (null != purchaseRecords.get(i).getPaid())
                    map.put("paid", purchaseRecords.get(i).getPaid());
                else map.put("paid", "无");
                if (null != purchaseRecords.get(i).getMdSum())
                    map.put("mdSum", purchaseRecords.get(i).getMdSum());
                else map.put("mdSum", 0);
                if (null != purchaseRecords.get(i).getMdPrice())
                    map.put("mdPrice", purchaseRecords.get(i).getMdPrice());
                else map.put("mdPrice", 0);
                if (null != purchaseRecords.get(i).getPaymentUnit())
                    map.put("paymentUnit", purchaseRecords.get(i).getPaymentUnit());
                else map.put("paymentUnit", "无");
                if (null != purchaseRecords.get(i).getPaymentUnit())
                    map.put("purchaseTime", purchaseRecords.get(i).getPurchaseTime());
                else map.put("purchaseTime", "无");
                list.add(map);
            }
            Map map = new HashMap();
            map.put("data", list);//放入表一数据
            TemplateExportParams params = new TemplateExportParams("/home/ruoyi/excel/项目的所有采购材料.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException ioException) {

        }
    }


    //回时间段供应商付款信息
    @GetMapping("/purchaseInfo")
    public TableDataInfo purchaseInfo(PurchaseRecord purchaseRecord) {
        //开启分页
        startPage();
        List<PurchaseRecord> list = AnalysisService.purchaseInfo(purchaseRecord);
        return getDataTable(list);
    }

    //返回时间段供应商付款信息
    @GetMapping("/purchaseInfo/export")
    public void purchase(PurchaseRecord purchaseRecord, HttpServletResponse response) {
        List<PurchaseRecord> purchaseRecords = AnalysisService.purchaseInfo(purchaseRecord);
        List<Purchase> purchases = AnalysisService.selectSumPur(purchaseRecord);
        String pur_id="";
        Double niu=0.0;
        Double full=0.0;
        if (purchases!=null&&purchases.size()>0) {
            for (int i = 0; i < purchases.size(); i++) {
                pur_id += purchases.get(i).getId() + ",";
                niu += purchases.get(i).getBePaid() + purchases.get(i).getPaid()+purchases.get(i).getAftPaid();
                full+=purchases.get(i).getFullAmount();
            }
        }
        List<Object> list = new ArrayList<>();
        // 表一数据
        try {
            DecimalFormat df = new DecimalFormat("#.##");
            df.setRoundingMode(RoundingMode.HALF_UP);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < purchaseRecords.size(); i++) {
                Map<String, Object> map = new HashMap<>();
//                map.put("id", i + 1);
                if (null != purchaseRecords.get(i).getProName()) {
                    map.put("proName", purchaseRecords.get(i).getProName());
                } else map.put("proName", "无");
                if (null != purchaseRecords.get(i).getProNum()) {
                    map.put("proNum", purchaseRecords.get(i).getProNum());
                } else map.put("proNum", "无");
                if (null != purchaseRecords.get(i).getmName())
                    map.put("mName", purchaseRecords.get(i).getmName());
                else map.put("mName", "无");
                if (null != purchaseRecords.get(i).getMdSpecifications() && null != purchaseRecords.get(i).getMdSpecifications()) {
                    map.put("mdSpecifications", purchaseRecords.get(i).getMdSpecifications());
                } else map.put("mdSpecifications", "无");
                if (null != purchaseRecords.get(i).getMdSpecifications()) {
                    map.put("mModel", purchaseRecords.get(i).getmModel());
                } else map.put("mModel", "无");
                if (null != purchaseRecords.get(i).getmUnit())
                    map.put("mUnit", purchaseRecords.get(i).getmUnit());
                else map.put("mUnit", "无");
                if (null != purchaseRecords.get(i).getMdBrand())
                    map.put("mdBrand", purchaseRecords.get(i).getMdBrand());
                else map.put("mdBrand", "无");
                if (null != purchaseRecords.get(i).getMdBusiness())
                    map.put("mdBusiness", purchaseRecords.get(i).getMdBusiness());
                else map.put("mdBusiness", "无");
                if (null != purchaseRecords.get(i).getPaid())
                    map.put("paid", purchaseRecords.get(i).getPaid());
                else map.put("paid", "无");
                if (null != purchaseRecords.get(i).getMdSum())
                    map.put("mdSum", purchaseRecords.get(i).getMdSum());
                else map.put("mdSum", 0);
                if (null != purchaseRecords.get(i).getMdPrice())
                    map.put("mdPrice", purchaseRecords.get(i).getMdPrice());
                else map.put("mdPrice", 0);
                if (null != purchaseRecords.get(i).getPaymentUnit())
                    map.put("paymentUnit", purchaseRecords.get(i).getPaymentUnit());
                else map.put("paymentUnit", "无");
                if (null != purchaseRecords.get(i).getPaymentUnit())
                    map.put("purchaseTime", purchaseRecords.get(i).getPurchaseTime());
                else map.put("purchaseTime", "无");
                list.add(map);
            }
            Map map = new HashMap();
            map.put("data", list);//放入表一数据
            map.put("pur_id",pur_id);
            map.put("dint_paid",full-niu);
            map.put("need_paid",full);
            TemplateExportParams params = new TemplateExportParams("/home/ruoyi/excel/供应商付款信息.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException ioException) {

        }
    }


}
